package com.cn.demo4.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.common.LoadImg;
import com.cn.demo4.common.Tool;
import com.cn.demo4.entity.NewsEntity;
import com.cn.demo4.entity.PageEntity;
import com.cn.demo4.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class NewsController {
    @Autowired
    private INewsService iNewsService;
    @RequestMapping("/admin/news/findAll")
    @ResponseBody
    public void findAll(HttpServletResponse resp, NewsEntity newsEntity, HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        String status = req.getParameter("status");
        status = status == null ? "-1": status;
        newsEntity.setStatus(Tool.stringToInt(status));
        PageEntity pageEntity = new PageEntity();
        pageEntity.setCurPage(Tool.toCurPage(req.getParameter("curPage")));
        pageEntity.setPageSize(Tool.toPageSize(req.getParameter("pageSize")));
        newsEntity.setPageEntity(pageEntity);
        List<NewsEntity> list = iNewsService.findAll(newsEntity);
        int count = iNewsService.getCount(newsEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/news/findById")
    @ResponseBody
    public void findById(HttpServletResponse resp, NewsEntity newsEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        NewsEntity model = iNewsService.findById(newsEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/news/add")
    @ResponseBody
    public void add(HttpServletResponse resp, NewsEntity newsEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        newsEntity.setTitle((String) req.getAttribute("title"));
        newsEntity.setContent((String) req.getAttribute("content"));
        newsEntity.setImg((String) req.getAttribute("img"));
        newsEntity.setTypeId(Tool.stringToInt((String)req.getAttribute("typeId")) );
        newsEntity.setTypeName((String) req.getAttribute("typeName"));
        newsEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        newsEntity.setIsShow(Tool.stringToInt((String) req.getAttribute("isShow")));
        newsEntity.setPublisher((String) req.getAttribute("publisher"));
        String result = iNewsService.add(newsEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/news/update")
    @ResponseBody
    public void update(HttpServletResponse resp, NewsEntity newsEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        newsEntity.setId(Tool.stringToInt((String)req.getAttribute("id")) );
        newsEntity.setTitle((String) req.getAttribute("title"));
        newsEntity.setContent((String) req.getAttribute("content"));
        newsEntity.setImg((String) req.getAttribute("img"));
        newsEntity.setTypeId(Tool.stringToInt((String)req.getAttribute("typeId")) );
        newsEntity.setTypeName((String) req.getAttribute("typeName"));
        newsEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        newsEntity.setIsShow(Tool.stringToInt((String) req.getAttribute("isShow")));
        newsEntity.setPublisher((String) req.getAttribute("publisher"));
        String result = iNewsService.update(newsEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/news/del")
    @ResponseBody
    public void del(HttpServletResponse resp, NewsEntity newsEntity) throws IOException {
        int result = iNewsService.del(newsEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
//    @RequestMapping("/admin/news/getNavModelListForHome")
//    @ResponseBody
//    public void getNavModelListForHome(HttpServletResponse resp, NewsEntity newsEntity,HttpServletRequest req) throws IOException {
//        Tool.setEncoding(req,resp);
//        List<NewsEntity> list = iNewsService.getNavModelListForHome(newsEntity);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("list",list);
//        resp.getWriter().println(jsonObject);
//    }
    @RequestMapping("/findDetailsById")
    @ResponseBody
    public void findByDetailsId(HttpServletResponse resp, NewsEntity newsEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        List<NewsEntity> list = iNewsService.findDetailsById(newsEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        resp.getWriter().println(jsonObject);
    }

}
